#!/usr/bin/env python
# encoding: utf-8

"""
@version: 0.1
@author: Kouen
@license: Apache Licence
@email: jobkouen@outlook.com
@software: PyCharm Community Edition
@file: stock_spiders.py
@time: 2016/12/30 下午2:32
@简介:
    
"""
import os
import sys

sys.path.insert(0, os.getcwd()[:os.getcwd().find("AnalySignalSystem")]+"AnalySignalSystem")

from scrapy.contrib.spiders import Spider
from source.Report.Scrapy.sina.sina.items import SinaCodeItem
import scrapy


class SinaFinan(Spider):
    name = 'sina_stock_code'

    def start_requests(self):
        for p in range(1, 110):
            url = "http://stock.finance.sina.com.cn/stock/go.php/vIR_RatingNewest/index.phtml?p=%d" % p
            yield scrapy.Request(url=url, callback=self.all_stock_parse)

    def all_stock_parse(self, response):
        item = SinaCodeItem()
        for i in response.xpath('//table[@class="list_table"]/descendant::tr')[1:]:
            item['code'] = i.xpath('descendant::a/text()').extract()[0]
            item['name'] = i.xpath('descendant::span/text()').extract()[0]
            item['url'] = i.xpath("td/descendant::a/@href").extract()[0]
            yield item

if __name__ == '__main__':
    pass
    # print os.getcwd()[:os.getcwd().find("AnalySignalSystem")]+"AnalySignalSystem"
